package com.nulldev.util.web.HttpServer3;

public class ByteReadRange {
	private final static long BRR_IGNORED = -1L;

	public final long min;
	public final long max;

	public ByteReadRange(final long min, final long max) {
		this.min = min;
		this.max = max;
	}

	public long length() {
		/* Return one if MIN and MAX are identical */
		if (this.min == this.max)
			return 1;
		/* Return MIN if we have no MAX value */
		if (this.max == BRR_IGNORED)
			return this.min;
		/* Return MAX if we have no MIN value */
		if (this.min == BRR_IGNORED)
			return this.max;
		/* Else do (MAX - MIN) + 1 */
		return this.max - this.min + 1;
	}

	public long toRead() {
		return length();
	}

	public boolean hasMax() {
		return this.max > 0;
	}

	@Override
	public String toString() {
		final boolean min_ignored = (this.min == BRR_IGNORED);
		final boolean max_ignored = (this.max == BRR_IGNORED);
		return "ByteReadRange[min=" + (min_ignored ? "IGNORED" : this.min) + ",max=" + (max_ignored ? "IGNORED" : this.max) + ",amount=" + toRead() + "]";
	}

	public String format() {
		if (this.max <= 0)
			return this.min + "-";
		return min + "-" + max;
	}
}
